package com.lyshop.jinxiang.view;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.application.MyApplication;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.OrderInfoBean;
import com.lyshop.jinxiang.core.AppAction;
import com.lyshop.jinxiang.net.ActionCallbackListener;
import com.lyshop.jinxiang.utils.ToastUtil;

import java.util.ArrayList;

public class OrderUpdateDialog extends MyDialog implements View.OnClickListener {

    private View rootView;
    private View view_top;
    private TextView tv_cancel;
    private TextView tv_submit;
    private OrderInfoBean orderBean;
    private RedSeekBar sbr_up;
    private GreenSeekBar sbr_down;

    private BaseActivity mActivity;
    private AppAction mAppAction;

    private EditOrderListener mListener;

    public OrderUpdateDialog(BaseActivity activity, OrderInfoBean bean) {
        this(activity, false, Gravity.BOTTOM, bean);
    }

    public OrderUpdateDialog(BaseActivity activity, boolean isCancelAble, int gravity, OrderInfoBean bean) {
        super(activity, isCancelAble, gravity);
        this.mActivity = activity;
        this.orderBean = bean;
        this.mAppAction = ((MyApplication) activity.getApplicationContext()).getAppAction();
        Window w = this.getWindow();
        if (w != null) {
            w.setWindowAnimations(R.style.dialogTranslateAnim);
        }
        rootView = LayoutInflater.from(activity.getBaseContext()).inflate(R.layout.dialog_update_order, null);
        setContentView(rootView);

        sbr_up = rootView.findViewById(R.id.sbr_up);
        sbr_down = rootView.findViewById(R.id.sbr_down);
        view_top = rootView.findViewById(R.id.view_top);
        tv_cancel = rootView.findViewById(R.id.tv_cancel);
        tv_submit = rootView.findViewById(R.id.tv_submit);

        tv_cancel.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
        view_top.setOnClickListener(this);

        setSeekBarMaxMinValue();
    }

    public int removeElement(int[] nums, int val) {
        ArrayList newNums = new ArrayList<Integer>();
        for(int i=0; i<nums.length; i++){
            int num = nums[i];
            if(num != val){
                int temp;
                temp = nums[i];
                nums[i] = nums[i+1];
                nums[i+1] = temp;
                newNums.add(num);
            }
        }
        return newNums.size();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_top:
                if (isCancelAble) {
                    dismiss();
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_submit:
                onSubmit();
                break;
        }
    }

    private void setSeekBarMaxMinValue() {
        int maxStop;
        int minStop;
        int maxTarget;
        int minTarget;

        int default_target;
        int default_stop;

        if(orderBean.use_ticket == 1){
            maxTarget = orderBean.max_ticket_target_profit;
            minTarget = orderBean.min_ticket_target_profit;
            maxStop = orderBean.max_ticket_stop_loss;
            minStop = orderBean.min_ticket_stop_loss;
        }else{
            maxTarget = orderBean.max_target_profit;
            minTarget = orderBean.min_target_profit;
            maxStop = orderBean.max_stop_loss;
            minStop = orderBean.min_stop_loss;
        }

        default_target = orderBean.target_profit;
        default_stop = orderBean.stop_loss;

        sbr_up.setMaxMinValue(maxTarget, minTarget, false);
        sbr_down.setMaxMinValue(maxStop, minStop, false);
        sbr_up.setValue(default_target);
        sbr_down.setValue(default_stop);

        sbr_up.setUnitPrice(orderBean.unit_price);
        sbr_down.setUnitPrice(orderBean.unit_price);
        sbr_up.setAmount(orderBean.amount);
        sbr_down.setAmount(orderBean.amount);
        sbr_up.setSingleProfit(orderBean.k_amount);
        sbr_down.setSingleProfit(orderBean.k_amount);

        sbr_up.calculateProfitAndLoss();
        sbr_down.calculateProfitAndLoss();
    }

    public void setListener(EditOrderListener listener){
        this.mListener = listener;
    }

    private void onSubmit() {
        mActivity.loading();
        mAppAction.editOrderInfo(orderBean.order_id, sbr_up.getValue(), sbr_down.getValue(), new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                dismiss();
                mActivity.cancelLoading();
                orderBean.target_profit = (sbr_up.getValue());
                orderBean.stop_loss = (sbr_down.getValue());
                mListener.onEditOrderSuccess();
                ToastUtil.showToast(mActivity, "设置成功");
            }

            @Override
            public void onFailure(String errorCode, String message) {
                mActivity.cancelLoading();
                ToastUtil.showToast(mActivity, message);
            }
        });
    }


    public interface EditOrderListener {

        void onEditOrderSuccess();

        void onEditOrderLogoutStatus();
    }

}

